#ifndef __MODBUS_RTU_MASTER_H__
#define __MODBUS_RTU_MASTER_H__

#include <modbus/modbus.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "modbus_handler.h"
#include "../iso_interface.h"
#include "../global_variable.h"
#include "../fan_control.h"

#define OLD_FAN 1
#define NEW_FAN 2
#define FAN_TYPE OLD_FAN

#if(FAN_TYPE == NEW_FAN)
enum 
{
    MC_HANDLE_INDEX_FAN1_SPEED_SET,         /* 风机1风速设置 */
    MC_HANDLE_INDEX_FAN1_END = MC_HANDLE_INDEX_FAN1_SPEED_SET,
    MC_HANDLE_INDEX_FAN2_SPEED_SET,         /* 风机2风速设置 */
    MC_HANDLE_INDEX_FAN2_END = MC_HANDLE_INDEX_FAN2_SPEED_SET,
    MC_HANDLE_INDEX_ISO_BOARD_DATA,         /* 绝缘检测板数据 */
    MC_HANDLE_INDEX_ISO_BOARD_FREQ_SET,         /* 绝缘检测板频率设置 */
    MC_HANDLE_INDEX_ISO_BOARD_CS_SET,         /* 绝缘检测板互感设置 */
    MC_HANDLE_INDEX_ISO_BOARD_CMD_SET,         /* 绝缘检测板检测命令设置 */

    MC_HANDLE_INDEX_QTY
};
#elif(FAN_TYPE == OLD_FAN)
enum 
{
    MC_HANDLE_INDEX_FAN1_BEGIN,
    MC_HANDLE_INDEX_FAN1_EN = MC_HANDLE_INDEX_FAN1_BEGIN,
    MC_HANDLE_INDEX_FAN1_SPEED_SET,         /* 风机1风速设置 */
    MC_HANDLE_INDEX_FAN1_END = MC_HANDLE_INDEX_FAN1_SPEED_SET,
    MC_HANDLE_INDEX_FAN2_BEGIN,
    MC_HANDLE_INDEX_FAN2_EN = MC_HANDLE_INDEX_FAN2_BEGIN,
    MC_HANDLE_INDEX_FAN2_SPEED_SET,         /* 风机2风速设置 */
    MC_HANDLE_INDEX_FAN2_END = MC_HANDLE_INDEX_FAN2_SPEED_SET,
    MC_HANDLE_INDEX_ISO_BOARD_DATA,         /* 绝缘检测板数据 */
    MC_HANDLE_INDEX_ISO_BOARD_FREQ_SET,         /* 绝缘检测板频率设置 */
    MC_HANDLE_INDEX_ISO_BOARD_CS_SET,         /* 绝缘检测板互感设置 */
    MC_HANDLE_INDEX_ISO_BOARD_CMD_SET,         /* 绝缘检测板检测命令设置 */

    MC_HANDLE_INDEX_QTY
};
#endif

typedef enum 
{
    OT_READ = 0,
    OT_WRITE = !OT_READ
} opt_type_t;

typedef void dms_t(modbus_t*);
typedef void dmr_t(modbus_t*,uint16_t*);

typedef struct 
{
    uint16_t indexNo;
    opt_type_t optType;
    dms_t *do_modbus_send;
    dmr_t *do_modbus_recv;
} mc_handle_t;

void ModbusRequest_Fan1TurnOn(modbus_t *ctx);
void ModbusRequest_Fan1Speed(modbus_t *ctx);
void ModbusRequest_Fan2TurnOn(modbus_t *ctx);
void ModbusRequest_Fan2Speed(modbus_t *ctx);
void ModbusRequest_ISOBoardAllInfor(modbus_t *ctx, uint16_t *pBuf);
void ModbusRequest_ISOBoardFreqSet(modbus_t *ctx);
void ModbusRequest_ISOBoardCsSet(modbus_t *ctx);
void ModbusRequest_ISOBoardCmdSet(modbus_t *ctx);
void master_comm_do(modbus_t *ctx, const mc_handle_t *pMcHandle);
void ParseModbusReply_ISOBoardAllInfor(uint16_t *pBuf);

#endif